package com.isunland.managesystem.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.daimajia.swipe.adapters.ArraySwipeAdapter;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.AttendanceDetail;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.List;

@SuppressWarnings("deprecation") public class AttendanceListAdapter
    extends ArraySwipeAdapter<AttendanceDetail> implements OnClickListener {

  public static final int TYPE_LOCALE_WORK = 2;
  public static final int TYPE_USER = 1;

  private int mType;
  private LayoutInflater mInflater;
  private Context mContext;
  private Callback mCallback;

  // Constructors
  public AttendanceListAdapter(Context context, List<AttendanceDetail> objects, Callback callback,
      int type) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mType = type;
    this.mContext = context;
    this.mCallback = callback;
  }

  @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_attendance_list, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    AttendanceDetail item = (AttendanceDetail) getItem(position);
    if (item == null) item = new AttendanceDetail();
    //Bind your data to the views here
    vh.mLLSwipe.setOnClickListener(this);
    vh.mLLSwipe.setTag(position);
    //作业任务-考勤人员查看隐藏删除按钮
    if (mType == TYPE_LOCALE_WORK) {
      vh.mLLSwipe.setVisibility(View.GONE);
    }
    String date = DateFormat.format("MM月dd EEEE", item.getDate()).toString();
    String time = DateFormat.format(" kk:mm", item.getCheckTime()).toString();
    switch (item.getCheckType()) {
      case 0:
        vh.mTvSignTimeItemAttendanceList.setText(
            MyStringUtil.connect(mContext.getString(R.string.sign_in),
                mContext.getString(R.string.lineBreak), time));
        vh.mTvSignTimeItemAttendanceList.setBackgroundColor(
            getContext().getResources().getColor(R.color.blue_textColor));
        break;
      case 1:
        vh.mTvSignTimeItemAttendanceList.setText(
            MyStringUtil.connect(mContext.getString(R.string.sign_out),
                mContext.getString(R.string.lineBreak), time));
        vh.mTvSignTimeItemAttendanceList.setBackgroundColor(
            getContext().getResources().getColor(R.color.primary_pink));
        break;
      case 2:
        vh.mTvSignTimeItemAttendanceList.setText(
            MyStringUtil.connect(mContext.getString(R.string.sign_field_time),
                mContext.getString(R.string.lineBreak), time));
        vh.mTvSignTimeItemAttendanceList.setBackgroundColor(
            getContext().getResources().getColor(R.color.yelloy_textColor));
        break;
    }
    vh.mTvDateItemAttendanceList.setText(date);
    String location = item.getLocation();
    if (location != null && !location.equals("")) {
      vh.mTvSignLocationItemAttendanceList.setVisibility(View.VISIBLE);
      vh.mTvSignLocationItemAttendanceList.setText(location);
    } else {
      vh.mTvSignLocationItemAttendanceList.setVisibility(View.GONE);
    }
    //当前登录人显示考勤分组,否则显示人员名称.
    //考勤分组,attendCardStatus为T打卡有效显示考勤分组
    if (mType == TYPE_USER) {

      showAttendGroup(vh, item);
    } else if (mType == TYPE_LOCALE_WORK) {
      showName(vh, item);
    } else {
      //其他情况显示考勤分组.
      showAttendGroup(vh, item);
    }

    String remarks = item.getRemark();// 内容

    if (!TextUtils.isEmpty(remarks)) {
      vh.mTvSignRemarkItemAttendanceList.setVisibility(View.VISIBLE);
      String mote = mContext.getString(R.string.note);// 备注：

      SpannableString span = new SpannableString(mote + remarks);
      span.setSpan(new ForegroundColorSpan(getContext().getResources().getColor(R.color.dark_note)),
          3, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

      vh.mTvSignRemarkItemAttendanceList.setText(span);
    } else {
      vh.mTvSignRemarkItemAttendanceList.setVisibility(View.GONE);
    }
    String filePath = item.getPhotoPath();// 打卡图片
    //if (position==4){
    //    filePath="100014_北京信普达系统工程有限公司/app/o_enterpriseforum_main/1F6E70A2-42AB-4CE2-A27B-E98D952765BF/4C81479F-76AD-4BF3-9CAA-EF61D744C0C5.jpg";
    //
    //}
    if (!TextUtils.isEmpty(filePath)) {
      vh.mIvPhoto.setVisibility(View.VISIBLE);
      vh.mIvPhoto.setTag(filePath);
      vh.mIvPhoto.setOnClickListener(this);
    } else {
      vh.mIvPhoto.setVisibility(View.GONE);
    }

    return vh.rootView;
  }

  private void showAttendGroup(ViewHolder vh, AttendanceDetail item) {
    if (!TextUtils.isEmpty(item.getAttendCardStatus()) && "T".equalsIgnoreCase(
        item.getAttendCardStatus())) {
      if (!TextUtils.isEmpty(item.getAttendGroupName())) {
        vh.mTvAttendGroupItemAttendanceList.setVisibility(View.VISIBLE);
        vh.mTvAttendGroupItemAttendanceList.setText(item.getAttendGroupName());
      } else {
        vh.mTvAttendGroupItemAttendanceList.setVisibility(View.GONE);
      }
    } else {
      vh.mTvAttendGroupItemAttendanceList.setVisibility(View.GONE);
    }
  }

  private void showName(ViewHolder vh, AttendanceDetail item) {
    vh.mTvAttendGroupItemAttendanceList.setText(item.getName());
  }

  @Override public int getSwipeLayoutResourceId(int arg0) {
    return R.id.swipe;
  }

  @Override public void onClick(View arg0) {
    mCallback.click(arg0);
  }

  /**
   * 自定义接口，用于回调按钮点击事件到Activity
   */
  public interface Callback {
    void click(View v);
  }

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-06-17 15:43:48 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    final TextView mTvDateItemAttendanceList;
    final TextView mTvSignTimeItemAttendanceList;
    final TextView mTvSignLocationItemAttendanceList;
    final TextView mTvSignRemarkItemAttendanceList;
    final TextView mTvAttendGroupItemAttendanceList;
    final ImageView mLLSwipe;
    ImageView mIvPhoto;

    private ViewHolder(LinearLayout rootView, TextView mTvDateItemAttendanceList,
        TextView mTvSignTimeItemAttendanceList, TextView mTvSignLocationItemAttendanceList,
        TextView mTvSignRemarkItemAttendanceList, TextView mTvAttendGroupItemAttendanceList,
        ImageView mLLSwipe, ImageView mIvPhoto) {
      this.rootView = rootView;
      this.mTvDateItemAttendanceList = mTvDateItemAttendanceList;
      this.mTvSignTimeItemAttendanceList = mTvSignTimeItemAttendanceList;
      this.mTvSignLocationItemAttendanceList = mTvSignLocationItemAttendanceList;
      this.mTvSignRemarkItemAttendanceList = mTvSignRemarkItemAttendanceList;
      this.mTvAttendGroupItemAttendanceList = mTvAttendGroupItemAttendanceList;
      this.mLLSwipe = mLLSwipe;
      this.mIvPhoto = mIvPhoto;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView mTvDateItemAttendanceList =
          (TextView) rootView.findViewById(R.id.tv_date_ItemAttendanceList);
      TextView mTvSignTimeItemAttendanceList =
          (TextView) rootView.findViewById(R.id.tv_SignTime_ItemAttendanceList);
      TextView mTvSignLocationItemAttendanceList =
          (TextView) rootView.findViewById(R.id.tv_SignLocation_ItemAttendanceList);
      TextView mTvAttendGroupItemAttendanceList =
          (TextView) rootView.findViewById(R.id.tv_AttendGroup_ItemAttendanceList);
      TextView mTvSignRemarkItemAttendanceList =
          (TextView) rootView.findViewById(R.id.tv_SignRemark_ItemAttendanceList);
      ImageView mLLSwipe = (ImageView) rootView.findViewById(R.id.ll_swipe);
      ImageView mIvPhoto = (ImageView) rootView.findViewById(R.id.iv_photo);

      return new ViewHolder(rootView, mTvDateItemAttendanceList, mTvSignTimeItemAttendanceList,
          mTvSignLocationItemAttendanceList, mTvSignRemarkItemAttendanceList,
          mTvAttendGroupItemAttendanceList, mLLSwipe, mIvPhoto);
    }
  }
}
